package magic.deckbuilder;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;

public class CardDBController {
	
	private static final String TABLE = MagicDeckDBStructure.TABLE_CARD;

	private SQLiteDatabase db;
	
	public CardDBController(SQLiteDatabase db) {
		this.db = db;
	}
	
	/** Insert une nouvelle carte dans la base de données
	 * 
	 * @param 	card		card to insert in the db
	 * @return				id of the now insert
	 */
	public long insert(Card card) {
		ContentValues values = new ContentValues();
		setContentValues(values, card);
		return db.insert(TABLE, null, values);
	}
	
	/**  Met à jour une carte dans la base de données
	 * 
	 * @param 	id				id of the card
	 * @param 	card			new card values
	 * @return					the number of rows affected
	 */
	public int update(int id, Card card) {
		ContentValues values = new ContentValues();
		setContentValues(values, card);
		return db.update(TABLE, values, Card.ID + " = " +id, null);
		
	}
	
	/**  Supprime une carte dans la base de données
	 * 
	 * @param 	id				id of the card
	 * @return					the number of rows affected if a whereClause is passed in, 0 otherwise. To remove all rows and get a count pass "1" as the whereClause
	 */
	public int remove(int id){
		//Suppression d'un livre de la BDD grâce à l'ID
		return db.delete(TABLE, Card.ID + " = " +id, null);
	}
	
	/**	Set all values of the card in the ContentValues
	 * 
	 * @param values
	 * @param card
	 */
	public void setContentValues(ContentValues values, Card card) {
		values.put(Card.CAPACITY, card.serializeCapacities());
		values.put(Card.COLOR, card.serializeColor());
		values.put(Card.COST, card.serializeCost());
		values.put(Card.NAME, card.serializeName());
		values.put(Card.PICTURE, card.serializePicture());
		values.put(Card.TYPE, card.serializeType());
	}
}
